package jsp;

import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/4/13 10:12
 * Version:1.0-SNAPSHOT
 * Description:
 */
@WebServlet("*.session")
public class SessionServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        if("/add.session".equals(path)){
            addSession(request,response);
        }
        if("/find.session".equals(path)){
            findSession(request,response);
        }
        if("/modify.session".equals(path)){
            modifySession(request,response);
        }
        if("/remove.session".equals(path)){
            removeSession(request,response);
        }
        if("/clean.session".equals(path)){
            cleanSession(request,response);
        }
    }

    public void cleanSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 在session中提供了两种删除方式
        // 方式二:直接销毁当前的session并创建一个新的session
        // 由于销毁的session与新的session并不是同一个
        // 因此,在销毁的session存储的所有数据均会丢失
        // 即:删除了原有session作用域中所有的数据
        request.getSession().invalidate();
        request.getRequestDispatcher("/jsp/session.jsp").forward(request,response);
    }

    public void removeSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 在session中提供了两种删除方式
        // 方式一:删除session中指定key的数据
        request.getSession().removeAttribute("msg");
        request.getRequestDispatcher("/jsp/session.jsp").forward(request,response);
    }

    public void modifySession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 修改的本质也是添加
        // 当遇到key一致的数据时,会进行覆盖
        request.getSession().setAttribute("msg","Hello JSP");
        request.getRequestDispatcher("/jsp/session.jsp").forward(request,response);
    }

    public void addSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(true);
//        HttpSession session = request.getSession(false);
        // session是一个作用域
        // 所谓添加,即表示向作用域中存储数据
        // 参数有两个
        // 参数一:数据的key,必须是一个字符串
        // 参数二:数据的value,类型是一个Object,可以是任意类型
        session.setAttribute("msg","Hello Session");
        User user = new User(1,"admin","123456","13812345678","江苏-南京");
        session.setAttribute("user",user);
        // 可以设置session生存时间,单位:秒
        // 如果其值为0并不是立刻过期,而是表示永不过期
        session.setMaxInactiveInterval(60);
        response.sendRedirect(request.getContextPath()+"/jsp/session.jsp");
    }

    public void findSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String msg = (String) request.getSession().getAttribute("msg");
        User user = (User) request.getSession().getAttribute("user");
        System.out.println("msg:"+msg);
        System.out.println("user:"+user);
    }

}
